const puppeteer = require("puppeteer");
var fs = require("fs");

async function getContentTitleAndAuthor() {
  var array = fs.readFileSync("./data.txt").toString().split("\n");
  for (i in array) {
    console.log('======================');
    var videoUrl = array[i];
    const browser = await puppeteer.launch({
      headless: false,
      defaultViewport: { width: 1366, height: 786 },
    });
    const page = await browser.newPage();
    //open new tab
    // videoUrl ="https://v.qq.com/x/page/h32556220zq.html"
    console.log('地址:' + videoUrl);
    await page.goto(videoUrl);
    let titleContent = "";
    try {
      await page.waitForSelector(
        "body > div:nth-child(1) > div.site_container.container_short.container_player.cf > div.container_inner > div.wrap_main > div.player_headline.player_headline_mood.cf > div > div.video_base._base > h1"
      );
      titleContent = await page.$eval(
        "body > div:nth-child(1) > div.site_container.container_short.container_player.cf > div.container_inner > div.wrap_main > div.player_headline.player_headline_mood.cf > div > div.video_base._base > h1",
        (el) => el.innerText
      );
      console.log('标题:' + titleContent);
    } catch (e) {
      console.log("获取不到标题");
      console.log(e);
      await browser.close();
      continue;
    }

    //get search area then input keywords
    await page.type("#keywords", titleContent);
    // click search button
    await page.click(".btn_inner");
    try {
      await page.waitForSelector(
        "body > div.search_container > div.wrapper > div.wrapper_main > div:nth-child(2) > div > div:nth-child(1) > div.info_item.info_item_even > span.content"
      );
    } catch (e) {
      console.log("获取不到作者了,估计是因为搜索结果是其他内容");
      await browser.close();
      continue;
    }
    let h2 = await page.$eval(
      "body > div.search_container > div.wrapper > div.wrapper_main > div:nth-child(2) > div > div:nth-child(1) > div.info_item.info_item_even > span.content",
      (el) => el.innerText
    );
    console.log('作者:' + h2);
    await browser.close();
  }
}

// read url info
getContentTitleAndAuthor()
